package com.haier.dreambox.demo.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.widget.*;
import com.haier.dreambox.demo.MyApplication;
import com.haier.dreambox.demo.R;
import com.haier.dreambox.demo.bean.UnbindResultBean;
import com.haier.dreambox.demo.bean.uplus.Device;
import com.haier.dreambox.demo.net.HaierRestClientCommon;
import com.haier.dreambox.demo.persistence.DatabaseHelper;
import com.haier.dreambox.demo.utils.DialogUtil;
import com.haier.dreambox.demo.utils.MyPrefs_;
import com.haier.dreambox.demo.utils.ToastUtils;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import org.androidannotations.annotations.*;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.springframework.http.ResponseEntity;

import java.util.List;

/**
 * Created by smalant on 15/1/6.
 */
@EActivity(R.layout.activity_dev_unbind)
public class UnBindActivity extends Activity {

    @ViewById(R.id.id_radio_group)
    RadioGroup radioGroup;

    @ViewById(R.id.id_button_unbind)
    Button button;

    @ViewById(R.id.id_imageButton_common_head_back)
    ImageButton imageButton_back;

    @ViewById(R.id.id_textView_common_head_center_text)
    TextView textView_title;

    @App
    MyApplication application;

    @Pref
    MyPrefs_ pref;

    @RestService
    HaierRestClientCommon restClient;

    @Bean
    ToastUtils toastUtils;

    @OrmLiteDao(helper = DatabaseHelper.class)
    RuntimeExceptionDao<Device, String> deviceDao;

    ProgressDialog dialog;

    @AfterViews
    void afterViews() {

        textView_title.setText("解绑");

        List<Device> devices = application.getCachedUserDevices();
        int i = 0;
        for (Device device : devices) {
            RadioButton radioButton = new RadioButton(this);
            radioButton.setText(device.name);
            radioButton.setId(i);
            radioGroup.addView(radioButton, i);
            i++;
        }

        dialog = DialogUtil.buildProgressDialog(this, "解除绑定中...", null);


    }


    @Click(R.id.id_imageButton_common_head_back)
    void backClick() {
        if (userDeviceChangeSize > 0) {
            setResult(RESULT_OK, null);
        }
        finish();
    }


    @Click(R.id.id_button_unbind)
    void unBind() {
        dialog.show();
        unbindDevice();
    }

    int userDeviceChangeSize = 0;

    @Background
    void unbindDevice() {
        List<Device> devices = application.getCachedUserDevices();
        String userId = pref.userId().get();
        int checkPosition = radioGroup.getCheckedRadioButtonId();
        String mac = devices.get(checkPosition).mac;
        ResponseEntity<UnbindResultBean> resultBean = restClient.unbindDevice(userId, mac, application.getSequenceId());
        if (resultBean != null) {
            UnbindResultBean bean = resultBean.getBody();
            if (bean != null) {
                if (bean.ok()) {
                    toastUtils.showShort(this, "解绑成功");
                    userDeviceChangeSize = deviceDao.delete(devices.get(checkPosition));
                    removeView(checkPosition);
                    application.getCachedUserDevices().remove(checkPosition);
                }
            } else {
                toastUtils.showShort(this, "解绑失败");
            }
        } else {
            toastUtils.showShort(this, "解绑失败");
        }
        dismissDialog();

    }

    @UiThread
    void removeView(int checkedRadioButtonId) {
        radioGroup.removeViewAt(checkedRadioButtonId);
    }

    @UiThread
    void dismissDialog() {
        dialog.dismiss();
    }

}
